#include "response.hpp"

#include <iostream>

namespace etcd {

Response::Response(const etcdv3::V3Response& reply) {
    _index = reply.get_index();
    _action = reply.get_action();
    _error_code = reply.get_error_code();
    _error_message = reply.get_error_message();
    if (reply.has_values()) {
        auto val = reply.get_values();
        for (unsigned int index = 0; index < val.size(); index++) {
            _values.push_back(Value(val[index]));
            _keys.push_back(val[index].kvs.key());
        }
    } else {
        _value = Value(reply.get_value());
    }

    _prev_value = Value(reply.get_prev_value());
}


Response::Response()
    : _error_code(0),
      _index(0) {
}

Response::Response(int error_code, char const* error_message)
    : _error_code(error_code),
      _error_message(error_message),
      _index(0) {
}

int Response::error_code() const {
    return _error_code;
}

std::string const& Response::error_message() const {
    return _error_message;
}

int Response::index() const {
    return _index;
}

std::string const& Response::action() const {
    return _action;
}

bool Response::is_ok() const {
    return error_code() == 0;
}

Value const& Response::value() const {
    return _value;
}

Value const& Response::prev_value() const {
    return _prev_value;
}

Values const& Response::values() const {
    return _values;
}

Value const& Response::value(int index) const {
    return _values[index];
}

Keys const& Response::keys() const {
    return _keys;
}

std::string const& Response::key(int index) const {
    return _keys[index];
}

}